#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMAX = 100;
const int KMAX = 30000;

struct student {
	char registration_number[15];
	int score;
	int location_number;
	int local_rank;
}S[KMAX+10];

bool cmp(student a, student b) {
	if (a.score != b.score) return a.score > b.score;
	return strcmp(a.registration_number, b.registration_number) < 0;
}

int main() {
	int n, num = 0, sum = 0;
	int k;
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		scanf("%d", &k);
		sum += k;
		for (int j = 0; j < k; j++) {
			scanf("%s%d", S[num].registration_number, &S[num].score);
			S[num].location_number = i+1;
			num++;
		}
		sort(S+num-k, S+num, cmp);  // 将同一个考场的考生进行排序
		S[num-k].local_rank = 1;
		for (int j = num-k+1; j < num; j++) {
			if (S[j].score == S[j-1].score) {
				S[j].local_rank = S[j-1].local_rank;
			} else {
				S[j].local_rank = j - num + k + 1;
			}
		}
	}
	
	sort(S, S+num, cmp);
	int rank = 1;
	printf("%d\n", sum);
	for (int i = 0; i < num; i++) {
		if (i > 0 && S[i].score != S[i-1].score) {
			rank = i + 1;
		}		
		printf("%s %d %d %d\n", S[i].registration_number, rank, S[i].location_number, S[i].local_rank);
	}

	return 0;
}









